"""
查找替换嵌套的 STR_TO_DATE 函数
"""

import logging
import re


def str_to_date_hour_concat(m):
    logging.debug(" >>>> change_str_to_date_hour_concat")
    logging.debug(m)
    return "to_timestamp(concat({},lpad('19',2,'0')),'yyyyMMddhh24')".format(str(m.group(3).strip()).replace("'", ""))


def rep_complex_fn_str_to_date(document_content):
    logging.info("rep_complex_fn_str_to_date")
    df_patten = re.compile(
        r"(STR_TO_DATE|str_to_date)\((CONCAT|concat)\((.*),(LPAD|lpad)\(\'19\',2,\'0\'\)\),\'%Y%m%d%H\'\)")
    pg_context = df_patten.sub(str_to_date_hour_concat, document_content)
    document_content = pg_context
    return document_content
